// packageCommunity/pages/file/index.js
import{comments} from "../../../js/modules/mall.js" 
import {router} from '../../../js/router'; 
import BasePage from '../../../js/common/base-page' 
import {api,util} from '../../../js/util'
var app = getApp();
const pageBehavior = require('../../../js/common/PageBehavior')

Page({
  behaviors: [pageBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    enableOss: api.enableOss,
    imgDomain: api.imgDomain,
    videoDomain:api.videoDomain,
    ParentIndex:0,//父子级
    searchVal:'',//搜索
    MeansList:[],
    iconImg:api.host+'/WebSupplier/Template/Default/Css/skin/Default/images/icon/',
    meansType:'',
    meansUrl:'',
    modelBox:false,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ParentIndex:options.id,
      searchVal:options.keyword
    })
    wx.setNavigationBarTitle({
      title: options.name,
    })
    this.getMeansList();
  },
  //资料列表
  getMeansList:function(){
    var that = this;
    var data = {ParentId:that.data.ParentIndex,keyword:that.data.searchVal};
    comments.getListFormoment(data,function(content){
      that.setData({
        MeansList:content
      })
    })
  },
  //下载资料
  bindFlie(e){
    var that = this;
    var url = e.currentTarget.dataset.url;
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var types = e.currentTarget.dataset.types;
    this.setData({
      meansType:type,
      meansUrl:that.data.imgDomain+url
    })
    if(type && types == 'File'){
      if(!url)return;
      if(type=='jpeg'||type=='jpg'||type=='png'||type=='bmp'||type=='gif'||type=='ico'||type == 'mp3'||type == 'mp4'){
        this.setData({
          modelBox:true
        })
      }else{
        that.uploadFile()
      }
    }else{
      wx.navigateTo({
        url: '/packageCommunity/pages/file/index?id='+id+'&keyword='+that.data.searchVal+'&name='+name,
      })
    }
  },
  uploadFile(){
    var that=this;
    var url = that.data.meansUrl
    var type = that.data.meansType
    console.log(url,'===');
    wx.showModal({
      title: '温馨提示',
      content: '是否确认是想要下载文件',
      success (res) {
        if (res.confirm) {
          wx.showLoading({title:'下载中'})
          wx.downloadFile({ 
            url: url.replace("http://", "https://"), //仅为示例，并非真实的资源
            success (res) {
              that.setData({
                filePath:res.tempFilePath
              })
              // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
              if (res.statusCode === 200) {
                if(type=='jpeg'||type=='jpg'||type=='png'||type=='bmp'||type=='gif'||type=='ico'){
                  wx.saveImageToPhotosAlbum({
                    filePath: that.data.filePath,
                    success(res) {
                      wx.hideLoading()
                      wx.showModal({
                        content:'图片已下载到相册',
                        showCancel:false,
                        confirmText:'好的',
                        confirmColor:'#333',
                        success:function(res){},
                        fail:function(res){}
                      })
                    },
                    fail: function (res) {
                      wx.showToast({
                        title:"下载失败",
                        icon:'none',
                        duration:2000
                      })
                    }
                  })
                }else if(type=="mp4" || type=="mp3"){
                  wx.setClipboardData({
                    data:url.replace("http://", "https://"),
                    success (res) {
                      wx.hideLoading();
                      util.tips('复制成功，请在浏览器中进行下载');
                    }
                  })
                }else{
                  wx.openDocument({
                    filePath:that.data.filePath,
                    success:function(res){wx.hideLoading()}
                  })
                }
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  bindCancel(){
    this.setData({
      modelBox:false
    })
  },
  iptSearch:function(e){
    this.setData({
      searchVal:e.detail.value
    })
  },
  btnSearch:function(){
    this.getMeansList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})